package com.hongxia.assetsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongxia.assetsys.common.ErrorCode;
import com.hongxia.assetsys.domain.AssertOrg;
import com.hongxia.assetsys.domain.Asset;
import com.hongxia.assetsys.domain.AssetCategory;
import com.hongxia.assetsys.domain.AssetLabel;
import com.hongxia.assetsys.exception.BusinessException;
import com.hongxia.assetsys.service.AssertOrgService;
import com.hongxia.assetsys.service.AssetCategoryService;
import com.hongxia.assetsys.service.AssetLabelService;
import com.hongxia.assetsys.service.AssetService;
import com.hongxia.assetsys.mapper.AssetMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Hongxia
* @description 针对表【_asset】的数据库操作Service实现
* @createDate 2024-01-06 10:34:09
*/
@Service
public class AssetServiceImpl extends ServiceImpl<AssetMapper, Asset>
implements AssetService{
    @Resource
    AssetMapper assetMapper;
    @Resource
    AssertOrgService assertOrgService;
    @Resource
    AssetCategoryService categoryService;
    @Resource
    AssetLabelService labelService;

    @Override
    public Integer addAsset(Asset asset) {
        if(asset == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String assetno = asset.getAssetno();
        String assetname = asset.getAssetname();
        //校验参数
        if(StringUtils.isBlank(assetno)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"资产编号为空");
        }
        if(StringUtils.isBlank(assetname)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"资产名为空");
        }
        boolean result = this.save(asset);
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"保存失败！");
        }

        return asset.getId();
    }

    @Override
    public List<Asset> getAssetByName(String assetName) {
        if(assetName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"资产名称为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assetname",assetName);
        List<Asset> assetList = assetMapper.selectList(queryWrapper);
        return assetList;
    }

    @Override
    public List<Asset> getAssetByLabel(String assetLabel) {
        if(assetLabel == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"资产标签为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assetLabel",assetLabel);
        List<Asset> assetList = assetMapper.selectList(queryWrapper);
        return assetList;
    }

    @Override
    public List<Asset> getAssetByCate(String assetCategory) {
        if(assetCategory == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"资产分类为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assetCategory",assetCategory);
        List<Asset> assetList = assetMapper.selectList(queryWrapper);
        return assetList;
    }

    @Override
    public List<Asset> getAssetByBelongOrg(String orgName) {
        if(orgName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"所属机构名称为空");
        }
        //根据组织名称获取orgid
        AssertOrg assetOrg = assertOrgService.getOrgListByName(orgName);
        if(assetOrg == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"所属机构不存在");
        }
        Integer orgId = assetOrg.getId();
        //根据orgid查询
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("belongOrg",orgId);
        List<Asset> assetList = assetMapper.selectList(queryWrapper);
        return assetList;
    }

    @Override
    public Integer addAssetCategory(AssetCategory assetCategory) {
        if(assetCategory == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String category = assetCategory.getAssetcategory();
        //校验参数
        if(StringUtils.isBlank(category)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"资产分类为空");
        }
        boolean result = categoryService.save(assetCategory);
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"保存失败！");
        }

        return assetCategory.getId();
    }

    @Override
    public Integer addAssetLabel(AssetLabel assetLabel) {
        if(assetLabel == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String category = assetLabel.getAssetlabel();
        //校验参数
        if(StringUtils.isBlank(category)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"资产标签为空");
        }
        boolean result = labelService.save(assetLabel);
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"保存失败！");
        }

        return assetLabel.getId();
    }
}
